package com.feng.zhihao.demo.springbatch.job;

import java.util.List;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.feng.zhihao.demo.springbatch.dao.BatchTaskHisMapper;
import com.feng.zhihao.demo.springbatch.dao.BatchTaskMapper;
import com.feng.zhihao.demo.springbatch.entity.BatchTask;
import com.feng.zhihao.demo.springbatch.entity.BatchTaskExample;

/**
 * @author LDX
 *
 */
@Component
@Configurable
@EnableScheduling
public class LoadJobJob{

	@Autowired
	Scheduler scheduler;
	@Autowired
	BatchTaskMapper batchTaskMapper;
	@Autowired
	BatchTaskHisMapper batchTaskHisMapper;

	public LoadJobJob() throws SchedulerException {
		super();
	}


	@Scheduled(cron = "0/5 * * * * ?")
	public void execute() throws SchedulerException {
		List<BatchTask> batchTaskList = batchTaskMapper.selectByExample(new BatchTaskExample());
		for (BatchTask batchTask : batchTaskList) {
			JobDetail job = scheduler.getJobDetail(new JobKey(batchTask.getTaskId()));
			if(job == null && new Integer(1).equals(batchTask.getTastState())) {
				//任务不存在，且数据库配置存在配置任务已启用
				Trigger trigger = TriggerBuilder.newTrigger()
						.withIdentity(batchTask.getTaskId())
						.withSchedule(CronScheduleBuilder.cronSchedule(batchTask.getCronExpression()))
						.build();
				
				JobDataMap jobDataMap=new JobDataMap();
				jobDataMap.put("batchTask_id", batchTask.getTaskId());
				
				JobDetail jobDetail = JobBuilder.newJob(TaskJob.class)
					    .withIdentity(batchTask.getTaskId())
					    .setJobData(jobDataMap)
					    .build();
				scheduler.scheduleJob(jobDetail, trigger);
			}else if(job == null && new Integer(0).equals(batchTask.getTastState())){
				//任务不存在，且数据库配置存在配置任务已废弃
			}else if(job != null && new Integer(1).equals(batchTask.getTastState())) {
				//任务存在， 且数据库配置已启用
				CronTriggerImpl trigger = (CronTriggerImpl) scheduler.getTrigger(new TriggerKey(batchTask.getTaskId()));
				if(! trigger.getCronExpression().equals(batchTask.getCronExpression())) {
					//如果数据库配置与当前触发时间表达式不匹配
					Trigger newTrigger = TriggerBuilder.newTrigger()
							.withIdentity(batchTask.getTaskId())
							.withSchedule(CronScheduleBuilder.cronSchedule(batchTask.getCronExpression()))
							.build();
					scheduler.rescheduleJob(new TriggerKey(batchTask.getTaskId()), newTrigger);
				}
			}else if(job != null && new Integer(0).equals(batchTask.getTastState())) {
				//任务存在， 且数据库配置已废弃
				scheduler.deleteJob(new JobKey(batchTask.getTaskId()));
			}
		}
	}
} 
